package com.yugao.fintech.draper.quartz.utils;

import com.yugao.fintech.draper.quartz.constants.ScheduleConstants;
import lombok.experimental.UtilityClass;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobKey;
import org.quartz.TriggerKey;

@UtilityClass
public class QuartzJobUtils {

    /**
     * 生成任务键对象key
     */
    public static JobKey getJobKey(Long jobId, String jobGroup) {
        return JobKey.jobKey(ScheduleConstants.TASK_CLASS_NAME + jobId, jobGroup);
    }

    /**
     * 生成任务触发对象key
     */
    public static TriggerKey getTriggerKey(Long jobId, String jobGroup) {
        return TriggerKey.triggerKey(ScheduleConstants.TASK_CLASS_NAME + jobId, jobGroup);
    }

    /**
     * 定时任务执行策略
     */
    public static CronScheduleBuilder handleCronScheduleMisfirePolicy(String misfirePolicy, CronScheduleBuilder cb) {
        switch (misfirePolicy) {
            case ScheduleConstants.MISFIRE_IGNORE_MISFIRES:
                // 立即触发执行
                return cb.withMisfireHandlingInstructionIgnoreMisfires();
            case ScheduleConstants.MISFIRE_FIRE_AND_PROCEED:
                // 触发一次执行
                return cb.withMisfireHandlingInstructionFireAndProceed();
            case ScheduleConstants.MISFIRE_DO_NOTHING:
                // 不触发立即执行
                return cb.withMisfireHandlingInstructionDoNothing();
            case ScheduleConstants.MISFIRE_DEFAULT:
            default:
                // 默认触发策略
                return cb;
        }
    }

}
